package com.kgis.map.geometry
{

	import com.kgis.map.Map;
	import com.kgis.map.geometry.basetype.Location;
	import com.kgis.map.geometry.basetype.Pixel;
	import com.kgis.map.geometry.basetype.Unit;
	import com.kgis.map.style.IStyle;
	import com.kgis.map.style.PolylineStyle;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.controls.Alert;
	import mx.printing.PrintAdvancedDataGrid;

	public class ArrowLineGeometry extends PolylineGeometry
	{
		private var _arrowWidth:Number = 5;
		private var _arrowLength:Number = 15;
		private var polylineStyle:PolylineStyle;
//		private var _sprite:Sprite;
//		private var _pointIndex:int;
//		private var _map:Map;
//		private var timer:Timer;
		
		public function ArrowLineGeometry()
		{
			super();
		}
		
		public function get arrowWidth():Number
		{
			return _arrowWidth;
		}
		
		public function set arrowWidth(value:Number):void
		{
			if (value !== _arrowWidth)
			{
				_arrowWidth = value;
				
			}
		}
		
		public function get arrowLength():Number
		{
			return _arrowLength;
		}
		
		public function set arrowLength(value:Number):void
		{
			if (value !== _arrowLength)
			{
				_arrowLength = value;
			}
		}
		
		override public function draw(sprite:Sprite, geoStyle:IStyle, map:com.kgis.map.Map):void 
		{
			super.draw(sprite,geoStyle,map);
			if(this.pointList)
			{
				
				if (geoStyle == null) {
					polylineStyle = PolylineStyle.getDefaultStyle();
				}
				else {
					polylineStyle = geoStyle as PolylineStyle;
				}
				
				
//				if(duration==0)
//				{
					for (var i:int= 0; i < this.pointList.length-1; i++) 
					{
						drawArrow(sprite,this.pointList[i+1],this.pointList[i],map);
					}
//				}
//				else
//				{
//					_sprite=sprite;
//					_pointIndex=0;
//					_map=map;
//					timer=new Timer(duration,this.pointList.length-1);
//					timer.addEventListener(TimerEvent.TIMER, TimerMethod);
//					timer.start();
//					
//				}
			}
		}
		
//		private function TimerMethod(event:TimerEvent):void
//		{
//			drawArrow(_sprite,this.pointList[_pointIndex+1],this.pointList[_pointIndex],_map);
//			_pointIndex=_pointIndex+1;
//			if(_pointIndex==this.pointList.length-1)
//			{
//				timer.stop();
//				duration=0;
//			}
//		}
		
		private function drawArrow(sprite:Sprite, p1:PointGeometry,p2:PointGeometry, map:Map):void
		{
			var x:Number, y:Number;
			var pix1:Pixel=map.getLayerPxFromLocation(new Location(p1.x, p1.y));
			var pix2:Pixel=map.getLayerPxFromLocation(new Location(p2.x, p2.y));
	
			
			var ang:Number = -Math.atan2(pix2.x - pix1.x, pix2.y - pix1.y);
			var cos:Number = Math.cos(ang);
			var sin:Number = Math.sin(ang);
			
			var ax:Number = pix1.x;
			var ay:Number = pix1.y;
			x = -_arrowWidth;
			y = _arrowLength;
			
			var bx:Number = x * cos - y * sin + pix1.x;
			var by:Number = x * sin + y * cos + pix1.y;
			x = _arrowWidth;
			y = _arrowLength;
			var cx:Number = x * cos - y * sin + pix1.x;
			var cy:Number = x * sin + y * cos + pix1.y;
			
			sprite.graphics.beginFill(polylineStyle.color, polylineStyle.alpha);
			sprite.graphics.moveTo(ax, ay);
			sprite.graphics.lineTo(bx, by);
			sprite.graphics.lineTo(cx, cy);
			sprite.graphics.lineTo(ax, ay);
			sprite.graphics.endFill();
		}
	}
}